package com.xy.zookeeper.curator;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryOneTime;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class CuratorTest {
    CuratorFramework client;
    private String zkConnUrl = "127.0.0.1:2181";

    @Before
    public void before() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.builder()
                .connectString(zkConnUrl)
                .sessionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                //.retryPolicy(new RetryOneTime(3000))
                .namespace("create")
                .build();
        client.start();
    }

    @After
    public void after() {
        client.close();
    }

    @Test
    public void create1() throws Exception {
        //新增节点
        System.out.println("create1开始");
        client.create()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath("/node1", "node1".getBytes());
        System.out.println("create1结束");
    }

    @Test
    public void create2() throws Exception {
        //自定义权限列表
        List<ACL> acls = new ArrayList<>();
        //授权模式与授权对象
        Id id = new Id("ip", "127.0.0.1");
        acls.add(new ACL(ZooDefs.Perms.ALL, id));
        client.create()
                .withMode(CreateMode.PERSISTENT)
                .withACL(acls)
                .forPath("/node2", "node2".getBytes());
        System.out.println("create2结束");
    }

    @Test
    public void watch() throws Exception {
        final NodeCache nodeCache = new NodeCache(client, "/create/node1");
        nodeCache.start();
        System.out.println("开始");
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println(nodeCache.getCurrentData().getPath());
                System.out.println(new String(nodeCache.getCurrentData().getData()));
            }
        });
        System.out.println("waiting...");
        Thread.sleep(50000);
        System.out.println("结束");
        nodeCache.close();
    }

    @Test
    public void testTransaction() throws Exception {
        client.inTransaction()
                .create().forPath("/node1", "node1".getBytes())
                .and()
                .create().forPath("/node2", "node2".getBytes())
                .and()
                .commit();
    }
}
